package handler

import (
	"github.com/gin-gonic/gin"

	"gitee.com/hp-huiw/my-go-core/helper"
	"gitee.com/hp-huiw/my-go-core/util"
)

type MapLocationHandler interface {
	QueryByCoordinate(ctx *gin.Context)
	QueryPoiByKeywords(ctx *gin.Context)
}

func NewMapLocationHandler() MapLocationHandler {
	return &mapLocationHandler{
		amapHelper: helper.NewAMapHelper(),
	}
}

// Matches /query-by-coordinate of http get action.
func (h *mapLocationHandler) QueryByCoordinate(ctx *gin.Context) {
	latitude, err := util.StringToFloat64(ctx.Query("latitude"))

	if err != nil {
		ctx.Error(err)
		return
	}

	longitude, err := util.StringToFloat64(ctx.Query("longitude"))

	if err != nil {
		ctx.Error(err)
		return
	}

	result := h.amapHelper.QueryByCoordinate(latitude, longitude)

	SendDataResponse(ctx, result)
}

// Matches /query-poi-by-keywords of http get action.
func (h *mapLocationHandler) QueryPoiByKeywords(ctx *gin.Context) {
	keywords := ctx.Query("keywords")
	result := h.amapHelper.QueryPoiByKeywords(keywords)

	SendDataResponse(ctx, result)
}

type mapLocationHandler struct {
	amapHelper helper.AMapHelper
}
